package com.example.zx.theline.lyjh.lyzjh.pub;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zx.theline.R;
import com.example.zx.theline.lyjh.pub.JhshReadActivity;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;

import java.util.HashMap;
import java.util.Map;

import GsonBean.SgcxzjhDetailBean;
import GsonBean.ShyjhxqBean;
import GsonBean.SuccessBean;
import Utils.MyToast;
import Utils.RefreshManager;
import app.MyApplication;
import base.BaseActivity;
import dialog.CenterDialog;
import network.NetWorkApi;
import network.ObjectHttpResponseHandler;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Jianqi on 2017\09\18 0018.
 *
 * @function 施工周计划已完成详情页面
 */

public class SgzjhYiwcDetailActivity extends BaseActivity implements View.OnClickListener {
    //按钮
    private TextView leftTextView,centerTextView, cancel, ckyjh, zf, xdzjh,btnShlc;

    //计划内容
    private TextView lb, rjhh, ydbh,  kssj, xm, dd, hb, lc, xmmc, sgnr, sgjx, blfh, aqfx, fxcs;
    //相关单位
    private TextView sgdw, sgdwfzr, jldw, jldwfzr, jsdw,jsdwfzr, cwzd, ddt, dxjz, sbgldw, qtdw, shcs, xmzgbm;
    //Data
    private SgcxzjhDetailBean mDetailBean;
    private String bh;
    private CenterDialog mCenterDialog, mXdzjhDialog;
    private LinearLayout blfh_layout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail_zjhywc);
        Intent intent = getIntent();
        bh = intent.getStringExtra("bh");
        InitView();
        InitListen();
        InitData();
    }

    @Override
    protected void InitView() {
        centerTextView = (TextView) findViewById(R.id.centerTextView);
        cancel = (TextView) findViewById(R.id.cancel);
        ckyjh = (TextView) findViewById(R.id.ckyjh);
        zf = (TextView) findViewById(R.id.zf);
        leftTextView = (TextView) findViewById(R.id.leftTextView);
        xdzjh = (TextView) findViewById(R.id.xdzjh);
        btnShlc = (TextView) findViewById(R.id.btnShlc);

        lb = (TextView) findViewById(R.id.lb);
        rjhh = (TextView) findViewById(R.id.rjhh);
        ydbh = (TextView) findViewById(R.id.ydbh);
        kssj = (TextView) findViewById(R.id.kssj);
        xm = (TextView) findViewById(R.id.xm);
        dd = (TextView) findViewById(R.id.dd);
        hb = (TextView) findViewById(R.id.hb);
        lc = (TextView) findViewById(R.id.lc);
        xmmc = (TextView) findViewById(R.id.xmmc);
        sgnr = (TextView) findViewById(R.id.sgnr);
        sgjx = (TextView) findViewById(R.id.sgjx);
        blfh = (TextView) findViewById(R.id.blfh);
        blfh_layout = (LinearLayout) findViewById(R.id.blfh_layout);
        aqfx = (TextView) findViewById(R.id.aqfx);
        fxcs = (TextView) findViewById(R.id.fxcs);

        sgdw = (TextView) findViewById(R.id.sgdw);
        sgdwfzr = (TextView) findViewById(R.id.sgdwfzr);
        jldw = (TextView) findViewById(R.id.jldw);
        jldwfzr = (TextView) findViewById(R.id.jldwfzr);
        jsdw = (TextView) findViewById(R.id.jsdw);
        jsdwfzr = (TextView) findViewById(R.id.jsdwfzr);
        cwzd = (TextView) findViewById(R.id.cwzd);
        ddt = (TextView) findViewById(R.id.ddt);
        dxjz = (TextView) findViewById(R.id.dxjz);
        sbgldw = (TextView) findViewById(R.id.sbgldw);
        qtdw = (TextView) findViewById(R.id.qtdw);
        shcs = (TextView) findViewById(R.id.shcs);
        xmzgbm = (TextView) findViewById(R.id.xmzgbm);
    }

    @Override
    protected void InitData() {
        centerTextView.setText("周计划详情");
        requestDetailData();
    }

    @Override
    protected void InitListen() {
        cancel.setOnClickListener(this);
        ckyjh.setOnClickListener(this);
        zf.setOnClickListener(this);
        leftTextView.setOnClickListener(this);
        xdzjh.setOnClickListener(this);
        btnShlc.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancel:
                finish();
                break;
            case R.id.ckyjh: //查看月计划
                readMonthPlan();
                break;
            case R.id.zf: //审核已完成中作废功能
                mCenterDialog = new CenterDialog(this, new CenterDialog.CenterClick() {
                    @Override
                    public void confirmListener() {
                        requestZuofei();
                    }

                    @Override
                    public void cancelListener() {
                        mCenterDialog.dismiss();
                    }
                });
                mCenterDialog.setMessage("您确定要作废该计划吗?");
                mCenterDialog.show();
                break;
            case R.id.xdzjh: //下达周计划
                mXdzjhDialog = new CenterDialog(this, new CenterDialog.CenterClick() {
                    @Override
                    public void confirmListener() {
                        requestXiadaZjh();
                    }

                    @Override
                    public void cancelListener() {
                        mXdzjhDialog.dismiss();
                    }
                });
                mXdzjhDialog.setMessage("您确定要下达该周计划吗?");
                mXdzjhDialog.show();
                break;
            case R.id.leftTextView:
                finish();
                break;
            case R.id.btnShlc:
                SgzjhCkshjlActivity.detailBean = mDetailBean;
                Intent intent = new Intent(SgzjhYiwcDetailActivity.this, SgzjhCkshjlActivity.class);
                startActivity(intent);
                break;

            default:
                break;
        }
    }


    //查看月计划功能  根据日计划对应月计划编号查询月计划详情
    private void readMonthPlan() {
        MyApplication.ShowWaitDialog(this);
        RequestParams mParams = new RequestParams();

        Map<String,String> map=new HashMap<>();
        map.put("bh", mDetailBean.getInfo().getYdbh());

        NetWorkApi.getZsyjhXx(map, new Callback<ShyjhxqBean>() {
                    @Override
                    public void onResponse(Call<ShyjhxqBean> call, Response<ShyjhxqBean> response) {
                        try {
                            ShyjhxqBean mBean = response.body();

                            if (mBean.getMsg().equals("success")) {
                                JhshReadActivity.ZsyjhxxShBean = mBean;
                                startActivity(new Intent(SgzjhYiwcDetailActivity.this, JhshReadActivity.class));
                            }
                        } catch (Exception e) {
                            MyToast.showFail();
                        }
                        MyApplication.HideWaitDialog();
                    }

                    @Override
                    public void onFailure(Call<ShyjhxqBean> call, Throwable t) {
                        MyToast.showFail();
                    }
                }
        );
    }

    //提交审核已完成中的下达周计划功能
    private void requestXiadaZjh() {

        Map<String,String> map=new HashMap<>();
        map.put("bh", mDetailBean.getInfo().getBh()); //周计划号

        MyApplication.ShowWaitDialog(this);
        NetWorkApi.requestSgzjhywcXdzjh(map, new Callback<SuccessBean>() {
                    @Override
                    public void onResponse(Call<SuccessBean> call, Response<SuccessBean> response) {
                        try {
                            responseData(response.body());
                        } catch (Exception e) {
                            MyToast.showFail();
                        }
                        MyApplication.HideWaitDialog();
                    }

                    @Override
                    public void onFailure(Call<SuccessBean> call, Throwable t) {
                        MyToast.showFail();
                    }
                }
        );
    }

    //提交审核已完成中的作废功能
    private void requestZuofei() {

        Map<String,String> map=new HashMap<>();
        map.put("bh", mDetailBean.getInfo().getBh()); //周计划号
        MyApplication.ShowWaitDialog(this);
        NetWorkApi.requestSgzjhywcZfData(map, new Callback<SuccessBean>() {
                    @Override
                    public void onResponse(Call<SuccessBean> call, Response<SuccessBean> response) {
                        try {
                            responseData(response.body());
                        } catch (Exception e) {
                            MyToast.showFail();
                        }
                        MyApplication.HideWaitDialog();
                    }

                    @Override
                    public void onFailure(Call<SuccessBean> call, Throwable t) {
                        MyToast.showFail();
                    }
                }
        );
    }

    //提交数据，提取出一个方法
    private void responseData(Object obj) {
        SuccessBean mSuccessBean = (SuccessBean) obj;
        if (mSuccessBean.getMsg() == null) {
            MyToast.showFail();

            return;
        }
        if (mSuccessBean.getMsg().equals("success")) {
            MyToast.showToast("设置成功");
            RefreshManager.Sgzjh.RefreshShywc();
            SgzjhYiwcDetailActivity.this.finish();
        } else {
            MyToast.showToast("设置失败");
        }
    }

    //请求查询周计划详情页面的数据
    private void requestDetailData() {
        Map<String,String> map=new HashMap<>();
        map.put("bh", bh);
        MyApplication.ShowWaitDialog(this);
        NetWorkApi.getSgzjhDetailData(map, new Callback<SgcxzjhDetailBean>() {
                    @Override
                    public void onResponse(Call<SgcxzjhDetailBean> call, Response<SgcxzjhDetailBean> response) {
                        try {
                            mDetailBean = response.body();
                            if (mDetailBean.getMsg().equals("success")) {
                                if (mDetailBean.getInfo() != null) { //有数据，渲染页面
                                    lb.setText(mDetailBean.getInfo().getLb());
                                    rjhh.setText(mDetailBean.getInfo().getBh());
                                    ydbh.setText(mDetailBean.getInfo().getYdbh());
                                    kssj.setText(mDetailBean.getInfo().getExsgrq());
                                    xm.setText(mDetailBean.getInfo().getXlxbList().get(0).getXlmc());
                                    dd.setText(mDetailBean.getInfo().getXlxbList().get(0).getSgdd());
                                    hb.setText(mDetailBean.getInfo().getXlxbList().get(0).getXbmc());
                                    lc.setText(mDetailBean.getInfo().getXlxbList().get(0).getLic());
                                    xmmc.setText(mDetailBean.getInfo().getXmmc());
                                    sgnr.setText(mDetailBean.getInfo().getNrmc());
                                    sgjx.setText(mDetailBean.getInfo().getJxmc());

                                    if (mDetailBean.getInfo().getLb().equals("C")) {
                                        blfh_layout.setVisibility(View.GONE);
                                    } else {
                                        blfh_layout.setVisibility(View.VISIBLE);
                                        blfh.setText(mDetailBean.getInfo().getBlfhmc());
                                    }
                                    aqfx.setText(mDetailBean.getInfo().getAqfxmc());
                                    fxcs.setText(mDetailBean.getInfo().getAqfxcsmc());
                                    sgdw.setText(mDetailBean.getInfo().getSgdw());
                                    sgdwfzr.setText(mDetailBean.getInfo().getSgdwfzr());
                                    jldw.setText(mDetailBean.getInfo().getJldw());
                                    jldwfzr.setText(mDetailBean.getInfo().getJldwfzr());
                                    jsdw.setText(mDetailBean.getInfo().getJsdw());
                                    jsdwfzr.setText(mDetailBean.getInfo().getJsdwfzr());
                                    cwzd.setText(mDetailBean.getInfo().getCwzd());
                                    ddt.setText(mDetailBean.getInfo().getDdt());
                                    dxjz.setText(mDetailBean.getInfo().getSgdxjz());
                                    sbgldw.setText(mDetailBean.getInfo().getSbgldw());
                                    qtdw.setText(mDetailBean.getInfo().getQtdw());
                                    shcs.setText(mDetailBean.getInfo().getShcs());
                                    xmzgbm.setText(mDetailBean.getInfo().getXmzgbm());
                                } else {
                                    Toast.makeText(SgzjhYiwcDetailActivity.this, "暂无更多数据", Toast.LENGTH_SHORT).show();
                                }
                            } else {
                                MyToast.showNullObjectError(SgzjhYiwcDetailActivity.this);
                            }
                        } catch (Exception e) {
                            MyToast.showFail();
                        }
                        MyApplication.HideWaitDialog();
                    }

                    @Override
                    public void onFailure(Call<SgcxzjhDetailBean> call, Throwable t) {
                        MyToast.showFail();
                    }
                }
        );
    }

}
